#!/bin/bash
#author 51yuki
#date 2018.1.11
#Decription install extension module
yum -y install unzip zip patch &>/dev/null
Module=$1
Version=$2
SoftDir=/usr/local/src
InstallDir=/data/application/
URL="https://gitee.com/louis1986/shell_scripts/raw/master/web/nginx/Extension_module/"
cd ${InstallDir}/nginx/sbin && 
Compile_Parameters="$(./nginx -V 2>&1 |  grep configure | awk "{\$1=\$2=\"\";print \$0}")"
if [ $# -ne 2 ];then
	echo "Usage: $0 <Module> <Nginx_Version>"
	exit 1
fi
if [ "${Module}" == "nginx_upstream_check_module-master" ];then
	cd $SoftDir && wget ${URL}/${Module}.zip
	cd ${SoftDir}/nginx-${Version} && patch -p1 < ${SoftDir}/${Module}/check_${Version}+.patch
fi
function install_module() {
	cd $SoftDir  && [ ! -d ${Module}.zip ] && wget ${URL}/${Module}.zip
	unzip $Module
	echo ${Compile_Parameters}
	cd ${SoftDir}/nginx-${Version} && ./configure ${Compile_Parameters} --add-module=/usr/local/src/${Module} && make
	systemctl stop nginx.service && cd ${InstallDir}/nginx/sbin && mv nginx nginx.ori_$(date +%F) 
	cd ${SoftDir}/nginx-${Version} && cp objs/nginx ${InstallDir}/nginx/sbin/ && systemctl start nginx.service
}
install_module

